//
//  MainViewIcon.m
//  EpointControlDemo
//
//  Created by 李亚春 on 13-5-23.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "MainViewIcon.h"
#import "EpointPhoneViewController.h"
#import "CustomBadge.h"

@implementation MainViewIcon

- (id)initWithFrame:(CGRect)frame title:(NSString *)title img_normal:(NSString *)_img_normal img_highlight:(NSString *)_img_highlight viewController:(Class)controller context:(UIViewController *)_context
{
    self = [super initWithFrame:frame];
    if (self) {
        context = _context;
        disClass = controller;
        UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
        [bt setImage:[UIImage imageNamed:_img_normal] forState:UIControlStateNormal];
        //[bt setImage:[UIImage imageNamed:_img_highlight] forState:UIControlStateHighlighted];
        UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, 60, 20)];
        lb.text = title;
        lb.textAlignment = UITextAlignmentCenter;
        [lb setFont:[UIFont systemFontOfSize:12]];
        lb.backgroundColor = [UIColor clearColor];
        [self addSubview:bt];
        [self addSubview:lb];
        
        
        
        
        
        [bt setExclusiveTouch:YES];
        [bt addTarget:self action:@selector(pushView) forControlEvents:UIControlEventTouchUpInside];
        
        viewtitle = title;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame title:(NSString *)title img_normal:(NSString *)_img_normal img_highlight:(NSString *)_img_highlight viewController:(Class)controller context:(UIViewController *)_context tip:(NSString *)_tip;
{
    self = [self initWithFrame:frame title:title img_normal:_img_normal img_highlight:_img_highlight viewController:controller context:_context];
    if (_tip.intValue>0) {
//        UILabel *tipView = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 20, 20)];
//        [tipView setBackgroundColor:[UIColor redColor]];
//        tipView.layer.cornerRadius = 10;
//        
//        //        tipView.layer.borderWidth = 1;//设置边框的宽度，当然可以不要
//        //        tipView.layer.borderColor = [[UIColor whiteColor] CGColor];//设置边框的颜色
//        //        tipView.layer.masksToBounds = YES;
//        [tipView setFont:[UIFont boldSystemFontOfSize:10]];
//        [tipView setTextColor:[UIColor whiteColor]];
//        tipView.textAlignment = UITextAlignmentCenter;
//        tipView.text = _tip;
        cb = [CustomBadge customBadgeWithString:_tip];
        [cb setFrame:CGRectMake(60-cb.frame.size.width, 0, cb.frame.size.width, 25)];
        [self addSubview:cb];
        
    }
   return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSLog(@"touchesEnded");
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesCancelled");
}

#pragma mark Table view methods
- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf {
    // build single chat bubble cell with given text
    UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
    returnView.backgroundColor = [UIColor clearColor];
    
    UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"bubbleSelf":@"bubble" ofType:@"png"]];
    UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
    
    UIFont *font = [UIFont systemFontOfSize:12];
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
    
    
    
    UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width+10, size.height+10)];
    bubbleText.backgroundColor = [UIColor clearColor];
    bubbleText.font = font;
    bubbleText.numberOfLines = 0;
    bubbleText.lineBreakMode = UILineBreakModeCharacterWrap;
    bubbleText.text = text;
    
    bubbleImageView.frame = CGRectMake(0.0f, 0.0f, 200.0f, size.height+40.0f);
    if(fromSelf)
        returnView.frame = CGRectMake(120.0f, 10.0f, 200.0f, size.height+50.0f);
    else
        returnView.frame = CGRectMake(0.0f, 10.0f, 200.0f, size.height+50.0f);
    
    [returnView addSubview:bubbleImageView];
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width+10, size.height+10)];
    [img setImage:[UIImage imageNamed:@"bubble.png"]];
    
    [returnView addSubview:img];
    [bubbleImageView release];
    [returnView addSubview:bubbleText];
    [bubbleText release];
    
    return [returnView autorelease];
}

-(void)pushView
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:viewtitle forKey:@"viewtitle"];
    if ([viewtitle isEqualToString:@"通知公告"]) {
        [params setObject:@"1" forKey:@"isTzgg"];
    }
    EpointPhoneViewController *v = [[disClass alloc]initWithParams:params];
    [context.navigationController pushViewController:v animated:YES];
}

@end
